﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace Taxes.DAL
{
    /// <summary>
    /// 数据访问类:industry_info
    /// </summary>
    public partial class industry_info
    {
        public industry_info()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "industry_info");
        }

        public List<Model.industry_info> GetAllBigIndustry()
        {
            var sql = "SELECT * FROM industry_info where parent_id != 0 and len(industry_no)=4";

            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            var infos = new List<Model.industry_info>();
            foreach (DataRow row in dt.Rows)
            {
                Taxes.Model.industry_info model = new Taxes.Model.industry_info();
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["industry_no"] != null && row["industry_no"].ToString() != "")
                {
                    model.industry_no = row["industry_no"].ToString();
                }
                if (row["industry_name"] != null && row["industry_name"].ToString() != "")
                {
                    model.industry_name = row["industry_name"].ToString();
                }
                if (row["parent_id"] != null && row["parent_id"].ToString() != "")
                {
                    model.parent_id = Convert.ToInt32(row["parent_id"]);
                }
                infos.Add(model);
            }

            return infos;
        }

        public List<Model.industry_info> GetIndustryByParentID(int id)
        {
            var sql = "SELECT * FROM industry_info where parent_id=" + id;

            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            var infos = new List<Model.industry_info>();
            foreach (DataRow row in dt.Rows)
            {
                Taxes.Model.industry_info model = new Taxes.Model.industry_info();
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["industry_no"] != null && row["industry_no"].ToString() != "")
                {
                    model.industry_no = row["industry_no"].ToString();
                }
                if (row["industry_name"] != null && row["industry_name"].ToString() != "")
                {
                    model.industry_name = row["industry_name"].ToString();
                }
                if (row["parent_id"] != null && row["parent_id"].ToString() != "")
                {
                    model.parent_id = Convert.ToInt32(row["parent_id"]);
                }
                infos.Add(model);
            }

            return infos;
        }

        public List<Model.industry_info> GetTopIndustry()
        {
            var sql = "SELECT * FROM industry_info where parent_id=0";

            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            var infos = new List<Model.industry_info>();
            foreach (DataRow row in dt.Rows)
            {
                Taxes.Model.industry_info model = new Taxes.Model.industry_info();
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["industry_no"] != null && row["industry_no"].ToString() != "")
                {
                    model.industry_no = row["industry_no"].ToString();
                }
                if (row["industry_name"] != null && row["industry_name"].ToString() != "")
                {
                    model.industry_name = row["industry_name"].ToString();
                }
                if (row["parent_id"] != null && row["parent_id"].ToString() != "")
                {
                    model.parent_id = Convert.ToInt32(row["parent_id"]);
                }
                infos.Add(model);
            }

            return infos;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from industry_info");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Taxes.Model.industry_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into industry_info(");
            strSql.Append("industry_no,industry_name,parent_id)");
            strSql.Append(" values (");
            strSql.Append("@industry_no,@industry_name,@parent_id)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@industry_no", SqlDbType.VarChar,10),
					new SqlParameter("@industry_name", SqlDbType.VarChar,50),
					new SqlParameter("@parent_id", SqlDbType.Int,4)};
            parameters[0].Value = model.industry_no;
            parameters[1].Value = model.industry_name;
            parameters[2].Value = model.parent_id;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Taxes.Model.industry_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update industry_info set ");
            strSql.Append("industry_no=@industry_no,");
            strSql.Append("industry_name=@industry_name,");
            strSql.Append("parent_id=@parent_id");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@industry_no", SqlDbType.VarChar,10),
					new SqlParameter("@industry_name", SqlDbType.VarChar,50),
					new SqlParameter("@parent_id", SqlDbType.Int,4),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.industry_no;
            parameters[1].Value = model.industry_name;
            parameters[2].Value = model.parent_id;
            parameters[3].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from industry_info ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteTopType(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from industry_info ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            strSql = new StringBuilder();
            strSql.Append("delete from industry_info ");
            strSql.Append(" where parent_id=@ID");
            SqlParameter[] parameters1 = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            int rows1 = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters1);
            if (rows > 0 && rows1 >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from industry_info ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Taxes.Model.industry_info GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,industry_no,industry_name,parent_id from industry_info ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            Taxes.Model.industry_info model = new Taxes.Model.industry_info();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["industry_no"] != null && ds.Tables[0].Rows[0]["industry_no"].ToString() != "")
                {
                    model.industry_no = ds.Tables[0].Rows[0]["industry_no"].ToString();
                }
                if (ds.Tables[0].Rows[0]["industry_name"] != null && ds.Tables[0].Rows[0]["industry_name"].ToString() != "")
                {
                    model.industry_name = ds.Tables[0].Rows[0]["industry_name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["parent_id"] != null && ds.Tables[0].Rows[0]["parent_id"].ToString() != "")
                {
                    model.parent_id = Convert.ToInt32(ds.Tables[0].Rows[0]["parent_id"]);
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,industry_no,industry_name,parent_id ");
            strSql.Append(" FROM industry_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,industry_no,industry_name,parent_id ");
            strSql.Append(" FROM industry_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method
    }
}

